<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <link rel="stylesheet" href="//unpkg.com/element-ui@2.13.2/lib/theme-chalk/index.css">
</head>
<body class="hold-transition">

<div id="app">

    <div class="content-header">
        <h1>系统管理
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>系统管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">

            <div class="filter-container">
                <el-input placeholder="菜单名称" style="width: 200px;" class="filter-item"
                          v-model="pagination.queryString"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建菜单</el-button>
            </div>
            <el-table
                    :data="list"
                    style="width: 100%"
                    row-key="id"
                    lazy
                    border
                    ref="tableDom"
                    :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
                <el-table-column type="index" align="center" label="编号"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="链接路径" align="center"></el-table-column>
                <el-table-column prop="path" label="菜单层级" align="center"></el-table-column>
                <el-table-column prop="priority" label="优先级" align="center"></el-table-column>
                <el-table-column prop="icon" label="图标" align="center"></el-table-column>
                <el-table-column prop="description" label="描述" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row.id)">修改</el-button>
                        <el-button type="danger" size="mini" @click="handleDelete(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                    class="pagiantion"
                    @current-change="handleCurrentChange"
                    :current-page="pagination.currentPage"
                    :page-size="pagination.pageSize"
                    layout="total, prev, pager, next, jumper"
                    :total="pagination.total">
            </el-pagination>


            <div class="add-form">
                <el-dialog title="新建菜单" :visible.sync="dialogFormVisible">
                    <el-form ref="dataAddForm" :model="user" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="父菜单" prop="parentMenuId">
                                    <el-select v-model="user.parentMenuId">
                                        <!--<el-option label="跟菜单" value="0"></el-option>
                                        <el-option label="工作台" value="1"></el-option>
                                        <el-option label="会员管理" value="2"></el-option>
                                        <el-option label="预约设置" value="3"></el-option>
                                        <el-option label="健康评估" value="4"></el-option>-->
                                        <el-option
                                                v-for="val in username"
                                                :label="val.name"
                                                :value="val.id"
                                        ></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="user.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="链接路径" prop="linkUrl">
                                    <el-input v-model="user.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="优先级" prop="icon">
                                    <el-input type="number" v-model="user.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="图标">
                                    <el-input v-model="user.icon"></el-input>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="描述">
                                    <el-input v-model="user.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="addUser()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <div class="edit-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataEditForm" :model="user" label-position="right" label-width="100px">
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="父菜单" prop="parentMenuId">
                                    <el-select v-model="user.parentMenuId">
                                        <el-option
                                                v-for="val in username"
                                                :label="val.name"
                                                :value="val.id"
                                        ></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="菜单名称" prop="name">
                                    <el-input v-model="user.name"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="链接路径" prop="linkUrl">
                                    <el-input v-model="user.linkUrl"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="优先级" prop="icon">
                                    <el-input type="number" v-model="user.priority"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="图标">
                                    <el-input v-model="user.icon"></el-input>
                                </el-form-item>
                            </el-col>

                        </el-row>
                        <el-row>
                            <el-col :span="22">
                                <el-form-item label="描述">
                                    <el-input v-model="user.description" type="textarea"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>

    </div>
</div>


</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui@2.13.2/lib/index.js"></script>
<script>
    var app = new Vue({
        el: '#app',
        data: {
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            list: [],
            user: {},
            dialogFormVisible: false,
            dialogFormVisible4Edit: false,
            roleList: [],
            roleIds: [],
            username: []
        },
        mounted() {
            this.findPage();
        },
        methods: {
            handleUpdate(id) {
                //设置编辑窗口属性，dialogFormVisible4Edit为true显示
                this.dialogFormVisible4Edit=true;
                //弹出编辑窗口
                axios.get("/menu/findAllLevel1.do").then((res)=>{
                    if (res.data.flag){
                        //将检查项列表数据赋值给模型数据用于页面表格展示
                        this.username=res.data.data;
                    }else {
                        this.$message.error(res.data.message);
                    }
                });
                //回显数据，发送ajax请求，根据检查项id提获取当前检查项的信息
                axios.get("/menu/findById.do?id="+id).then((res)=>{
                    if (res.data.flag){

                        //为模型数据设置值，基于vue双向数据绑定进行回显
                        this.user=res.data.data;

                    }else {
                        this.$message.error("获取数据失败，请刷新当前页面");
                    }
                })

            },
            // 重置表单
            resetForm() {
                //重置表单数据
                this.user={};

            },
            handleCreate() {
                this.dialogFormVisible = true;
                this.resetForm();

                axios.get("/menu/findAllLevel1.do").then((res)=>{
                    if (res.data.flag){
                        //将检查项列表数据赋值给模型数据用于页面表格展示
                        this.username=res.data.data;
                    }else {
                        this.$message.error(res.data.message);
                    }
                })
            },
            addUser() {
                //发送ajax请求将模型数据提交到后台处理
                axios.post("/menu/add.do",this.user).then(res=>{
                    //关闭新增窗口
                    this.dialogFormVisible = false;
                    if(response.data.flag){
                        //新增成功，弹出成功提示
                        this.$message({
                            message: response.data.message,
                            type: 'success'
                        });
                    }else{
                        //新增失败，弹出错误提示
                        this.$message.error(response.data.message);
                    }
                }).finally(()=>{
                    this.findPage();
                })
            },
            handleEdit() {
                //表单校验通过，可以提交数据
                axios.post("/menu/edit.do",this.user).then((res)=>{
                    //关闭编辑窗口
                    this.dialogFormVisible4Edit=false;
                    if (res.data.flag){
                        //成功，弹出成功提示信息
                        this.$message({
                            type:'success',
                            message:res.data.message
                        });
                    } else {
                        //执行失败
                        this.$message.error(res.data.message);
                    }
                }).finally(()=>{
                    //不管成功还是失败，都要进行分页查询的操作
                    this.findPage();
                })
            },
            findPage() {
                //封装分页参数
                let param = {
                    currentPage:this.pagination.currentPage,
                    pageSize:this.pagination.pageSize,
                    queryString:this.pagination.queryString
                };
                //发送ajax请求，提交分页相关参数
                axios.post("/menu/findPage.do",param).then((res)=>{
                    //为模型数据赋值，基于VUE的双向绑定展示到页面
                    this.pagination.total=res.data.total;
                    /*if (this.pagination.total <= 10) {
                        this.handleCurrentChange(1);
                    }*/
                    this.list=res.data.rows;
                })
            },
            handleCurrentChange(currentPage) {
                //currentPage为切换后的页码
                this.pagination.currentPage=currentPage;
                this.findPage();
            },
            handleDelete(id){
                this.$confirm("确认删除吗？","提示",{type:'warning'})
                    .then(()=>{//用户点击的是确认按钮
                        //点击确定时执行此处代码，发送ajax请求，将检查项id提交到controller进行处理
                        axios.get("/menu/delete.do?id="+id).then((res)=>{
                            if (!res.data.flag){
                                //删除失败
                                this.$message.error(res.data.message);
                            }else {
                                //删除成功
                                this.$message({
                                    message: res.data.message,
                                    type:'success'
                                });
                                //调用分页查询语句重新查询
                                this.findPage();
                            }
                        }).catch((res)=>{
                            this.showMessage(res);
                        });
                    }).catch(()=>{//用户按了取消按钮
                    this.$message({
                        message: '操作已取消',
                        type:'info'
                    });
                })
            }
        }
    })
</script>
</html>